package com.icode.core.dto.house;

import com.icode.core.model.house.*;
import com.icode.core.shared.ImageUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-11-12
 * Time: 上午9:52
 * To change this template use File | Settings | File Templates.
 */
public class HouseInfoDTO {
    private String guid;
    private String name;
    private String houseFeature;
    private List<EquipmentDTO> equipmentDTOs;
    private List<EquipmentDTO> supportingDTOs;
    private String price;
    private List<String> equipmentAsStrings;
    private List<String> supportingAsStrings;
    private List<String> specialAsStrings;
    private MultipartFile image;
    private List<MultipartFile> images;
    private List<String> imageGuids;
    private String buildingTypeGuid;
    private String buildingTypeName;
    private String buildingTypeArea;
    private List<HouseWeekDTO> houseWeekDTOs;
    private List<HouseExtraFeeDTO> houseExtraFeeDTOs;
    private String title;
    private MultipartFile titleImage;
    private String titleImageGuid;
    private boolean fineRoom;
    private String content;
    private String houseType;
    private String houseAddress;
    private String personNum;

    public boolean isFineRoom() {
        return fineRoom;
    }

    public void setFineRoom(boolean fineRoom) {
        this.fineRoom = fineRoom;
    }

    public HouseInfoDTO() {
    }

    public HouseInfoDTO(String houseInfoName, String guid) {
        this.name = houseInfoName;
        this.guid = guid;
    }

    public HouseInfoDTO(String guid, String name, List<String> imageGuids) {
        this.guid = guid;
        this.name = name;
        this.imageGuids = imageGuids;
    }

    public HouseInfoDTO(String guid, String name, String buildingTypeName) {
        this.guid = guid;
        this.name = name;
        this.buildingTypeName = buildingTypeName;
    }

//    public HouseInfoDTO(List<EquipmentDTO> equipmentDTOs, List<EquipmentDTO> supportingDTOs, String buildingTypeGuid) {
//        this.equipmentDTOs = equipmentDTOs;
//        this.supportingDTOs = supportingDTOs;
//        this.buildingTypeGuid = buildingTypeGuid;
//
//    }

    public HouseInfoDTO(List<EquipmentDTO> equipmentDTOs, List<EquipmentDTO> supportingDTOs, String buildingTypeGuid) {
        this.equipmentDTOs = equipmentDTOs;
        this.supportingDTOs = supportingDTOs;
        this.buildingTypeGuid = buildingTypeGuid;
    }

    public void setImages(List<MultipartFile> images) {
        this.images = images;
    }

    public List<MultipartFile> getImages() {
        return images;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHouseFeature() {
        return houseFeature;
    }

    public void setHouseFeature(String houseFeature) {
        this.houseFeature = houseFeature;
    }

    public List<EquipmentDTO> getEquipmentDTOs() {
        return equipmentDTOs;
    }

    public void setEquipmentDTOs(List<EquipmentDTO> equipmentDTOs) {
        this.equipmentDTOs = equipmentDTOs;
    }

    public List<EquipmentDTO> getSupportingDTOs() {
        return supportingDTOs;
    }

    public void setSupportingDTOs(List<EquipmentDTO> supportingDTOs) {
        this.supportingDTOs = supportingDTOs;
    }


    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public List<String> getEquipmentAsStrings() {
        return equipmentAsStrings;
    }

    public void setEquipmentAsStrings(List<String> equipmentAsStrings) {
        this.equipmentAsStrings = equipmentAsStrings;
    }

    public List<String> getSupportingAsStrings() {
        return supportingAsStrings;
    }

    public List<HouseExtraFeeDTO> getHouseExtraFeeDTOs() {
        return houseExtraFeeDTOs;
    }

    public void setHouseExtraFeeDTOs(List<HouseExtraFeeDTO> houseExtraFeeDTOs) {
        this.houseExtraFeeDTOs = houseExtraFeeDTOs;
    }

    public String getTitleImageGuid() {
        return titleImageGuid;
    }

    public void setTitleImageGuid(String titleImageGuid) {
        this.titleImageGuid = titleImageGuid;
    }

    public void setSupportingAsStrings(List<String> supportingAsStrings) {
        this.supportingAsStrings = supportingAsStrings;
    }

    public List<String> getSpecialAsStrings() {
        return specialAsStrings;
    }

    public void setSpecialAsStrings(List<String> specialAsStrings) {
        this.specialAsStrings = specialAsStrings;
    }

    public List<String> getImageGuids() {
        return imageGuids;
    }

    public void setImageGuids(List<String> imageGuids) {
        this.imageGuids = imageGuids;
    }

    public MultipartFile getImage() {
        return image;
    }

    public void setImage(MultipartFile image) {
        this.image = image;
    }

    public String getBuildingTypeGuid() {
        return buildingTypeGuid;
    }

    public String getBuildingTypeName() {
        return buildingTypeName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public MultipartFile getTitleImage() {
        return titleImage;
    }

    public void setTitleImage(MultipartFile titleImage) {
        this.titleImage = titleImage;
    }

    public List<HouseWeekDTO> getHouseWeekDTOs() {
        return houseWeekDTOs;
    }

    public void setHouseWeekDTOs(List<HouseWeekDTO> houseWeekDTOs) {
        this.houseWeekDTOs = houseWeekDTOs;
    }

    public void setBuildingTypeName(String buildingTypeName) {
        this.buildingTypeName = buildingTypeName;
    }

    public void setBuildingTypeArea(String buildingTypeArea) {
        this.buildingTypeArea = buildingTypeArea;
    }

    public String getBuildingTypeArea() {
        return buildingTypeArea;
    }

    public void setBuildingTypeGuid(String buildingTypeGuid) {
        this.buildingTypeGuid = buildingTypeGuid;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getHouseType() {
        return houseType;
    }

    public void setHouseType(String houseType) {
        this.houseType = houseType;
    }

    public String getHouseAddress() {
        return houseAddress;
    }

    public void setHouseAddress(String houseAddress) {
        this.houseAddress = houseAddress;
    }

    public String getPersonNum() {
        return personNum;
    }

    public void setPersonNum(String personNum) {
        this.personNum = personNum;
    }

    public HouseInfo toHouseInfo(List<Equipment> equipments, List<Equipment> supportings, List<UploadImage> documents, BuildingType buildingType, HouseInfo houseInfo) {
        UploadImage titleImage = houseInfo.baseInfo().titleImage();
        if (!this.titleImage.isEmpty()) {
            titleImage = ImageUtils.MultipartFileToUploadImage(this.titleImage);
        }
        houseInfo.update(this.name, this.houseFeature, this.price, this.title, titleImage, fineRoom);
        List<HouseInfoEquipment> houseInfoEquipments = toHouseInfoEquipments(houseInfo, equipments);
        List<HouseInfoSupporting> houseInfoSupportings = toHouseInfoSupporting(houseInfo, supportings);
        List<HouseInfoImage> houseInfoImages = toHouseInfoImages(houseInfo, documents);
        for (HouseInfoImage houseInfoImage : houseInfo.houseInfoImages()) {
            houseInfoImages.add(houseInfoImage);
        }
        List<HouseExtraFee> houseExtraFees = toHouseExtraFee(houseInfo, houseExtraFeeDTOs);
        houseInfo.initEquipment(houseInfoEquipments, houseInfoSupportings, houseInfoImages, buildingType, houseExtraFees); //添加度假屋时 处理额外费用
        return houseInfo;
    }

    public HouseInfo toHouseInfo(HouseInfoDTO houseInfoDTO) {
        UploadImage titleImage = ImageUtils.MultipartFileToUploadImage(this.titleImage);
        HouseInfo houseInfo = new HouseInfo(houseInfoDTO.getName(), houseInfoDTO.getHouseFeature(), houseInfoDTO.getPrice(), houseInfoDTO.title, titleImage, fineRoom);
        return houseInfo;
    }

    public List<HouseInfo> toHouseInfos(List<HouseInfoDTO> houseInfoDTOs) {
        List<HouseInfo> houseInfos = new ArrayList<>();
        for (HouseInfoDTO h : houseInfoDTOs) {
            HouseInfo houseInfo = h.toHouseInfo(h);
            houseInfos.add(houseInfo);
        }
        return houseInfos;
    }

    private List<HouseInfoImage> toHouseInfoImages(HouseInfo houseInfo, List<UploadImage> documents) {
        List<HouseInfoImage> houseInfoImages = new ArrayList<>();
        for (UploadImage uploadImage : documents) {
            HouseInfoImage houseInfoImage = new HouseInfoImage(houseInfo, uploadImage);
            houseInfoImages.add(houseInfoImage);
        }
        return houseInfoImages;
    }

    private List<HouseExtraFee> toHouseExtraFee(HouseInfo houseInfo, List<HouseExtraFeeDTO> houseExtraFeeDTOs) {
        List<HouseExtraFee> list = new ArrayList<>();
        for (HouseExtraFeeDTO h : houseExtraFeeDTOs) {
            HouseExtraFee houseExtraFee = new HouseExtraFee(houseInfo, h.getTitle(), h.getPrice());
            list.add(houseExtraFee);
        }
        return list;
    }

    private List<HouseInfoEquipment> toHouseInfoEquipments(HouseInfo houseInfo, List<Equipment> equipments) {
        List<HouseInfoEquipment> list = new ArrayList<>();
        for (Equipment equipment : equipments) {
            HouseInfoEquipment houseInfoEquipment = new HouseInfoEquipment(houseInfo, equipment);
            list.add(houseInfoEquipment);
        }
        return list;
    }

    private List<HouseInfoSupporting> toHouseInfoSupporting(HouseInfo houseInfo, List<Equipment> equipments) {
        List<HouseInfoSupporting> list = new ArrayList<>();
        for (Equipment equipment : equipments) {
            HouseInfoSupporting houseInfoEquipment = new HouseInfoSupporting(houseInfo, equipment);
            list.add(houseInfoEquipment);
        }
        return list;
    }


    public HouseInfoDTO(String guid, String name, String houseFeature, List<EquipmentDTO> equipmentDTOs, List<EquipmentDTO> supportingDTOs,
                        String price, List<String> imageGuids, String buildingTypeGuid, boolean fineRoom) {
        this.guid = guid;
        this.name = name;
        this.houseFeature = houseFeature;
        this.equipmentDTOs = equipmentDTOs;
        this.supportingDTOs = supportingDTOs;
        this.price = price;
        this.imageGuids = imageGuids;
        this.buildingTypeGuid = buildingTypeGuid;
        this.fineRoom = fineRoom;
    }

    public HouseInfoDTO(String guid, String name, String houseFeature, List<EquipmentDTO> equipmentDTOs, List<EquipmentDTO> supportingDTOs,
                        String price, List<String> imageGuids, String buildingTypeGuid, String buildingTypeName, String buildingTypeArea,
                        List<HouseWeekDTO> houseWeekDTOs, List<HouseExtraFeeDTO> houseExtraFeeDTOs, String title, String titleImageGuid, boolean fineRoom, String houseType, String houseAddress, String personNum) {
        this.guid = guid;
        this.name = name;
        this.houseFeature = houseFeature;
        this.equipmentDTOs = equipmentDTOs;
        this.supportingDTOs = supportingDTOs;
        this.price = price;
        this.imageGuids = imageGuids;
        this.buildingTypeGuid = buildingTypeGuid;
        this.buildingTypeName = buildingTypeName;
        this.buildingTypeArea = buildingTypeArea;
        this.houseWeekDTOs = houseWeekDTOs;
        this.houseExtraFeeDTOs = houseExtraFeeDTOs;
        this.title = title;
        this.titleImageGuid = titleImageGuid;
        this.fineRoom = fineRoom;
        this.houseType = houseType;
        this.houseAddress = houseAddress;
        this.personNum = personNum;
    }

    public static List<HouseInfoDTO> toDTOs(List<HouseInfo> houseInfoList) {
        List<HouseInfoDTO> houseInfoDTOs = new ArrayList<>();
        for (HouseInfo houseInfo : houseInfoList) {
            HouseInfoDTO houseInfoDTO = toDTO(houseInfo);
            houseInfoDTOs.add(houseInfoDTO);
        }
        return houseInfoDTOs;
    }

    public static HouseInfoDTO toDTO(HouseInfo houseInfo) {
        HouseBaseInfo baseInfo = houseInfo.baseInfo();
        List<HouseInfoEquipment> houseInfoEquipment = houseInfo.equipment();
        List<HouseInfoSupporting> houseInfoSupportings = houseInfo.supporting();
        List<HouseInfoImage> houseInfoImages = houseInfo.houseInfoImages();
        List<String> imageGuids = initImageGuids(houseInfoImages);
        String guid = houseInfo.guid();
        String buildingTypeGuid = houseInfo.buildingType().guid();
        String buildingTypeName = houseInfo.buildingType().name();
        String buildingTypeArea = houseInfo.buildingType().area();
        String houseType = houseInfo.buildingType().houseType().getLabel();
        String houseAddress = "";
        if (houseInfo.baseInfo().province() != null && houseInfo.baseInfo().city() != null && houseInfo.baseInfo().district() != null) {
            houseAddress = houseInfo.baseInfo().province().name() + houseInfo.baseInfo().city().name() + houseInfo.baseInfo().district().name() + houseInfo.baseInfo().area();
        }
        List<EquipmentDTO> equipmentDTOs = EquipmentDTO.toDTOs(separateEquipments(houseInfoEquipment, null));
        List<EquipmentDTO> supportingDTOs = EquipmentDTO.toDTOs(separateEquipments(null, houseInfoSupportings));
        List<HouseWeekDTO> houseWeekDTOs = HouseWeekDTO.toDTOs(houseInfo.houseWeek());
        List<HouseExtraFeeDTO> houseExtraFeeDTOs = HouseExtraFeeDTO.toDTOs(houseInfo.houseExtraFees());
        String title = houseInfo.baseInfo().title();
        String titleImageGuid = "";
        boolean fineRoom = houseInfo.baseInfo().fineRoom();
        if (houseInfo.baseInfo().titleImage() != null) {
            titleImageGuid = houseInfo.baseInfo().titleImage().guid();
        }
        String personNum = houseInfo.buildingType().personNum();
        return new HouseInfoDTO(guid, baseInfo.houseName(), baseInfo.houseFeature(), equipmentDTOs, supportingDTOs, baseInfo.price(), imageGuids, buildingTypeGuid, buildingTypeName, buildingTypeArea, houseWeekDTOs, houseExtraFeeDTOs, title, titleImageGuid, fineRoom, houseType, houseAddress, personNum);
    }

    private static List<String> initImageGuids(List<HouseInfoImage> houseInfoImages) {
        List<String> imageGuids = new ArrayList<>();
        for (HouseInfoImage houseInfoImage : houseInfoImages) {
            String guid = houseInfoImage.image().guid();
            imageGuids.add(guid);
        }
        return imageGuids;
    }

    private static List<Equipment> separateEquipments(List<HouseInfoEquipment> houseInfoEquipments, List<HouseInfoSupporting> houseInfoSupportings) {
        List<Equipment> equipments = new ArrayList<>();
        List<Equipment> supportings = new ArrayList<>();
        if (houseInfoSupportings == null) {
            for (HouseInfoEquipment houseInfoEquipment : houseInfoEquipments) {
                Equipment equipment = houseInfoEquipment.equipment();
                if (!equipment.isActive()) {
                    break;
                }
                equipments.add(equipment);
            }
        } else {
            for (HouseInfoSupporting houseInfoEquipment : houseInfoSupportings) {
                Equipment equipment = houseInfoEquipment.equipment();
                if (!equipment.isActive()) {
                    break;
                }
                supportings.add(equipment);
            }
        }
        return equipments.isEmpty() ? supportings : equipments;
    }

    public HouseInfo toHouseInfo(List<Equipment> equipments, List<Equipment> supportings, List<UploadImage> documents, BuildingType buildingType) {
        UploadImage titleImage = ImageUtils.MultipartFileToUploadImage(this.titleImage);
        HouseInfo houseInfo = new HouseInfo(this.name, this.houseFeature, this.price, this.title, titleImage, fineRoom);
        List<HouseInfoEquipment> houseInfoEquipments = toHouseInfoEquipments(houseInfo, equipments);
        List<HouseInfoSupporting> houseInfoSupportings = toHouseInfoSupporting(houseInfo, supportings);
        List<HouseInfoImage> houseInfoImages = toHouseInfoImages(houseInfo, documents);
        List<HouseExtraFee> houseExtraFees = toHouseExtraFee(houseInfo, houseExtraFeeDTOs);
        houseInfo.initEquipment(houseInfoEquipments, houseInfoSupportings, houseInfoImages, buildingType, houseExtraFees); //添加度假屋时 处理额外费用
        return houseInfo;
    }

}
